package czy.demo.controller.crud;

import czy.demo.pojo.Person;
import czy.demo.repository.PersonRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.CrudRepository;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(value = "基本增删改查操作",tags = "基本增删改查操作")
@RestController
@RequestMapping("/crud")
public class CrudController implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(CrudController.class);

    @Autowired
    private PersonRepository personRepository;

    /* Crud接口提供了一般的增删改查方法 */
    private CrudRepository<Person,Integer> crudRepository;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.crudRepository = personRepository;
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取用户")
    public Person getPersonById(@PathVariable Integer id){
        Person person = this.crudRepository.findById(id).get();
        logger.debug("获取id为："+id+"用户为："+person);
        return person;
    }

    @GetMapping("/exist/{id}")
    @ApiOperation("用户是否存在")
    public boolean exist(@PathVariable Integer id){
        boolean exist = this.crudRepository.existsById(id);
        logger.debug("获取id为："+id+"用户是否存在："+exist);
        return exist;
    }

    @GetMapping("/count")
    @ApiOperation("获取用户数量")
    public Long getPersonCount(){
        return this.crudRepository.count();
    }

    /* 获取所有数据 */
    @GetMapping("/all")
    @ApiOperation("获取所有用户")
    public List<Person> getAllPerson(){
        List<Person> allPersons = (List<Person>)this.crudRepository.findAll();
        logger.debug("获取所有用户成功："+allPersons);
        return allPersons;
    }


    @PostMapping("/create")
    @ApiOperation("创建用户")
    public Person createPerson(@RequestBody Person person){
        Person newPerson = this.crudRepository.save(person);
        logger.debug("创建新用户："+newPerson);
        return newPerson;
    }

    @PostMapping("/create/batch")
    @ApiOperation("批量创建用户")
    public List<Person> createPersonWithBatch(@RequestBody List<Person> persons){
        List<Person> newPersons  = (List<Person>)this.crudRepository.saveAll(persons);
        logger.debug("批量创建新用户成功："+newPersons);
        return newPersons;
    }

    @PutMapping("/update")
    @ApiOperation("更新用户")
    public Person updatePerson(@RequestBody Person person){
        Person newPerson = this.crudRepository.save(person);
        logger.debug("更新用户成功："+person+"->"+newPerson);
        return newPerson;
    }

    @PutMapping("/update/batch")
    @ApiOperation("批量更新用户")
    public List<Person> updatePersonWithBatch(@RequestBody List<Person> persons){
        List<Person> newPersons = (List<Person>)this.crudRepository.saveAll(persons);
        logger.debug("批量更新用户成功："+persons+"->"+newPersons);
        return newPersons;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除指定ID用户")
    public String deletePersonById(@PathVariable Integer id){
        logger.debug("删除id为："+id+"用户");
        this.crudRepository.deleteById(id);
        return "根据id删除Person成功";
    }

    @DeleteMapping("/delete")
    @ApiOperation("获取指定用户")
    public String deletePersonByExample(@RequestBody Person person){
        logger.debug("删除用户："+person);
        this.crudRepository.delete(person);
        return "根据实体删除Person成功";
    }

    @DeleteMapping("/delete/all")
    @ApiOperation("删除所有用户")
    public String deleteAllPerson(){
        logger.debug("删除所有用户");
        this.crudRepository.deleteAll();
        return "删除所有Person成功";
    }


}
